package com.zeng.controller;


import com.zeng.mapper.DepartmentMapper;
import com.zeng.mapper.EmployeeMapper;
import com.zeng.pojo.Department;
import com.zeng.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author zeng
 * @date 2021/08/12 12:02
 */
@Controller
public class EployeeController {


    @Autowired
    DepartmentMapper departmentMapper;

    @Autowired
    EmployeeMapper employeeMapper;

    @RequestMapping("/emps")
    public String getEmployes(Model model) {

        Collection<Employee> employees = employeeMapper.getEmpInfo();
        model.addAttribute("emps", employees);
        return "emp/list";
    }

    @GetMapping("/Add")
    public String toAddEmployee(Model model) {
        //查看所有部门的信息,传到add.html页面上，展示部门所又部门
        Collection<Department> departments = departmentMapper.getDepartmentsInfo();
        model.addAttribute("departments", departments);
        return "emp/add";
    }

    @PostMapping("/Add")
    public String addEmployee(int id,String name, String email, Date birth, int sex, int department) {
        //添加员工信息,必须指定名称
        //Employee employee = new Employee(0, name, email, departmentMapper.getDepartment(department), sex);
        //System.out.println(employee);
        //employee.setDate(birth);
        employeeMapper.addEmp(id,name, email, birth, sex, department);
        return "redirect:/emps";
    }

    //员工修改页面
    @GetMapping("emp/{id}")
    public String toUpdateEmp(@PathVariable int id, Model model) {
        Employee employee = employeeMapper.getEmpById(id);
        model.addAttribute("emp", employee);
        Collection<Department> departments = departmentMapper.getDepartmentsInfo();
        model.addAttribute("departments", departments);
        return "emp/update";
    }

    //修改员工信息
    @PostMapping("/updateEmp")
    public String updateEmp(int id, String name, String email, Date birth, int sex, int department) {
        employeeMapper.delEmpById(id);

        //Employee employee = new Employee(id, name, email, departmentTable.getDepartmentById(department), sex);
        //employee.setDate(birth);
        //System.out.println(employee);
        //employeeTable.addEmployee(employee);
        employeeMapper.addEmp(id,name,email,birth,sex,department);
        System.out.println(1);
        return "redirect:/emps";
    }

    //删除员工
    @GetMapping("/delEmp/{id}")
    public String delEmp(@PathVariable int id) {
        employeeMapper.delEmpById(id);
        return "redirect:/emps";
    }

    //注销
    @GetMapping("/loginout")
    public String signOut(HttpSession session) {
        session.invalidate();
        return "redirect:/";
    }

    //整合mybatis
    @GetMapping("get")
    public String getdepart() {
        //System.out.println(departmentMapper.getDepartmentsInfo());
        System.out.println(departmentMapper.getDepartment(102));
        //System.out.println(employeeMapper.getEmpById(1001));;
        //System.out.println(employeeMapper.getEmpInfo());
        employeeMapper.delEmpById(1007);

        return "redirect:/emps";
    }
}
